<template>
    
    <el-form ref="formRef" class="pl-3" :model="formData" label-width="80px" inline>
        
        <el-card class="!border-none flex flex-1 items-center justify-center" shadow="never">
            <div class=" w-[800px]">
                <div class=" text-lg	 mb-4">
                    基本信息
                </div>
                
                <div class="pl-3 grid grid-cols-1 gap-0">
                    <div>
                        <el-form-item label="商品标题" prop="afterSaleOrderType"
                                      :rules="[{required: true, message:'工单类型不可为空',trigger:'change'}]">
                            
                            <el-input class="w-96">
                            
                            </el-input>
                        
                        
                        </el-form-item>
                    
                    
                    </div>
                    
                    <div>
                        <el-form-item label="商品标题" prop="afterSaleOrderType"
                                      :rules="[{required: true, message:'工单类型不可为空',trigger:'change'}]">
                            
                            <el-input class="w-96">
                            
                            </el-input>
                        
                        </el-form-item>
                    
                    
                    </div>
                    
                    <div>
                        <el-form-item label="商品标题" prop="afterSaleOrderType"
                                      :rules="[{required: true, message:'工单类型不可为空',trigger:'change'}]">
                            
                            <el-input class="w-96">
                            
                            </el-input>
                        
                        
                        </el-form-item>
                    
                    
                    </div>
                
                
                </div>
            </div>
        </el-card>
   
    </el-form>
    
    
    <footer-btns>
        <el-button type="primary" @click="handleSubmit">提交工单</el-button>

    
    </footer-btns>

</template>

<script lang='ts' setup>


import {ShallowRef, UnwrapNestedRefs} from "@vue/reactivity";
import {shallowRef} from "vue";
import {FormInstance} from "element-plus";

import feedback from "@/utils/feedback";
import {useAfterSaleEnumManager} from "@/manager/base/AfterSaleEnumManager";
import router from "@/base/router";
import useMultipleTabsManager from "@/manager/base/MultipleTabsManager";
import {useRoute} from "vue-router";
import {createSerialNumberString} from "@/api/afterSale/order/AfterSaleOrderListController";
import RouterManager from "@/manager/base/RouterManager";
import {
    afterSaleOrderInfoOrLineAddOne,
    AfterSaleOrderInfoOrLineDTO
} from "@/api/afterSale/order/dto/AfterSaleOrderInfoOrLineDTO";
import {useFormatterManager} from "@/manager/base/FormatterManager";
import {useAutocompleteQueryLineManager} from "@/manager/base/AutocompleteQueryManager";
import {AfterSaleItemMaterialDTO} from "@/api/afterSale/order/dto/AfterSaleItemMaterialDTO";
import {AfterSaleAddressInfoRequest} from "@/api/afterSale/order/request/AfterSaleAddressInfoRequest";
import {AfterSaleOrderTypeEnum} from "@/config/enums/aftersale/AfterSaleOrderTypeEnum";
import AfterSaleItemLineInfoDTO from "@/api/afterSale/order/dto/AfterSaleItemLineInfoDTO";
import {createAfterSaleOrder} from "@/api/afterSale/order/AfterSaleOrderController";


let afterSaleOrderConfig = useAfterSaleEnumManager();
let formatterManager = useFormatterManager(afterSaleOrderConfig);
let multipleTabsManager = useMultipleTabsManager();
let autocompleteQueryLineManager = useAutocompleteQueryLineManager(afterSaleOrderConfig);


const formRef: ShallowRef = shallowRef<FormInstance>()
const loading = ref("");


/**
 * 地址选择
 *
 * @param value
 */
function handleAddressChange(value: string[]) {
    formData.addressInfoEnum = value
    formData.addressInfo.economize = value[0]
    formData.addressInfo.city = value[1]
    formData.addressInfo.county = value[2]
}

/**
 * 编辑当前行
 *
 * @param row
 */
function editCurrentLine(row: any) {
    row.isDisabled = !row.isDisabled;
}


/**
 * 新增一个物料节点
 */
function handAddItemInfo() {
    formData.itemInfo.push(new AfterSaleItemLineInfoDTO())
}


/**
 *  工单提交
 *
 * @author 徐寿春
 * 2023/4/5 17:12
 */
function handleSubmit() {
    
    formRef.value?.validate().then(() => {
            // 如果是售后工单自定义校验
            if (formData.afterSaleOrderType == "RETURN_ORDER") {
                for (let itemLineInfos of formData.itemInfo) {
                    if (!itemLineInfos?.inMaterIalInfo?.itemName) {
                        feedback.notifyWarning("售后物料名称不可为空")
                        return
                    }
                    if (!itemLineInfos.inMaterIalInfo.itemCode) {
                        feedback.notifyWarning("售后物料编码不可为空")
                        return
                    }
                    if (!itemLineInfos.inMaterIalInfo.batch) {
                        feedback.notifyWarning("售后物料69码不可为空")
                        return
                    }
                    if (!itemLineInfos.inMaterIalInfo.sn) {
                        feedback.notifyWarning("售后物料SN不可为空")
                        return
                    }
                }
                
                
            } else if (formData.afterSaleOrderType == "EXCHANGE_ORDER") {
                for (let itemLineInfos of formData.itemInfo) {
                    if (!itemLineInfos?.inMaterIalInfo?.itemName) {
                        feedback.notifyWarning("售后物料名称不可为空")
                        return
                    }
                    if (!itemLineInfos.inMaterIalInfo.itemCode) {
                        feedback.notifyWarning("售后物料编码不可为空")
                        return
                    }
                    if (!itemLineInfos.inMaterIalInfo.batch) {
                        feedback.notifyWarning("售后物料69码不可为空")
                        return
                    }
                    if (!itemLineInfos.inMaterIalInfo.sn) {
                        feedback.notifyWarning("售后物料SN不可为空")
                        return
                    }
                    
                }
                
                for (let itemLineInfos of formData.itemInfo) {
                    if (!itemLineInfos?.outMaterialInfo?.itemName) {
                        feedback.notifyWarning("发出物料名称不可为空")
                        return
                    }
                    if (!itemLineInfos.outMaterialInfo.itemCode) {
                        feedback.notifyWarning("发出物料编码不可为空")
                        return
                    }
                    if (!itemLineInfos.outMaterialInfo.batch) {
                        feedback.notifyWarning("发出物料69码不可为空")
                        return
                    }
                }
            }
            
            
            createAfterSaleOrder(formData).then(() => {
                
                multipleTabsManager.removeTab()
                
                let routePathByName = RouterManager.getRoutePathByName("afterSaleOrder/order/orderList/index");
                if (!!routePathByName) {
                    router.push(routePathByName)
                }
                
            })
            
        }
    )
}


/**
 * 生成唯一序列号
 */
async function createSerialNumber() {
    await createSerialNumberString().then(resp => {
        formData.orderNumber = resp
    })
    
}


/**
 * 渠道值改变
 *
 * @author 徐寿春
 * 2023/4/12 11:00
 */
function handleChannelChange() {

}


/**
 * 测试
 *
 * @author 徐寿春
 * 2023/5/4 17:21
 */
async function testData() {
    formData.afterSaleOrderType = "RETURN_ORDER"
    formData.source = "XT"
    formData.channel = "OFFLINE"
    formData.shopName = "OFFLINE-01"
    formData.userName = "userName"
    formData.userPhone = "123132"
    formData.reason = "NOREASON"
    await createSerialNumberString().then(resp => {
        formData.logisticCompany = resp
    })
    await createSerialNumberString().then(resp => {
        formData.logisticNumber = resp
    })
    
    formData.logisticCompany = "顺丰"
    let afterSaleItemLineInfoDTO1 = new AfterSaleItemLineInfoDTO();
    let afterSaleItemLineInfoDTO2 = new AfterSaleItemLineInfoDTO();
    
    let afterSaleItemMaterialDTO = new AfterSaleItemMaterialDTO();
    afterSaleItemMaterialDTO.itemName = "测试物料"
    afterSaleItemMaterialDTO.itemCode = "XPPP232132"
    
    afterSaleItemMaterialDTO.batch = "4234234"
    await createSerialNumberString().then(resp => {
        afterSaleItemMaterialDTO.sn = resp
    })
    
    let afterSaleItemMaterialDTO2 = new AfterSaleItemMaterialDTO();
    afterSaleItemMaterialDTO2.itemName = "测试物料"
    afterSaleItemMaterialDTO2.itemCode = "XPPP232132"
    afterSaleItemMaterialDTO2.batch = "4234234"
    await createSerialNumberString().then(resp => {
        afterSaleItemMaterialDTO2.sn = resp
    })
    
    
    afterSaleItemLineInfoDTO1.inMaterIalInfo = afterSaleItemMaterialDTO
    afterSaleItemLineInfoDTO2.inMaterIalInfo = afterSaleItemMaterialDTO2
    formData.itemInfo[0] = afterSaleItemLineInfoDTO1
    formData.itemInfo.push(afterSaleItemLineInfoDTO2)
    
    await createSerialNumber()
}


async function testData2() {
    
    formData.afterSaleOrderType = "EXCHANGE_ORDER"
    formData.source = "XT"
    formData.channel = "OFFLINE"
    formData.shopName = "OFFLINE-01"
    formData.userName = "userName"
    formData.userPhone = "123132"
    formData.reason = "NOREASON"
    
    await createSerialNumberString().then(resp => {
        formData.logisticNumber = resp
    })
    
    
    formData.logisticCompany = "顺丰"
    
    
    let afterSaleAddressInfoRequest = new AfterSaleAddressInfoRequest();
    afterSaleAddressInfoRequest.userName = "徐寿春"
    afterSaleAddressInfoRequest.userPhone = "3123123123"
    afterSaleAddressInfoRequest.city = "蚌埠"
    afterSaleAddressInfoRequest.economize = "安徽"
    afterSaleAddressInfoRequest.county = "东市区"
    afterSaleAddressInfoRequest.address = "3123123"
    
    formData.addressInfo = afterSaleAddressInfoRequest
    formData.addressInfoEnum = ["安徽", "蚌埠", "东市区"]
    
    
    let afterSaleItemLineInfoDTO1 = new AfterSaleItemLineInfoDTO();
    let afterSaleItemLineInfoDTO2 = new AfterSaleItemLineInfoDTO();
    
    let afterSaleItemMaterialDTO = new AfterSaleItemMaterialDTO();
    afterSaleItemMaterialDTO.itemName = "测试物料"
    afterSaleItemMaterialDTO.itemCode = "XPPP232132"
    afterSaleItemMaterialDTO.batch = "4234234"
    await createSerialNumberString().then(resp => {
        afterSaleItemMaterialDTO.sn = resp
    })
    
    let afterSaleItemMaterialDTO1_1 = new AfterSaleItemMaterialDTO();
    
    afterSaleItemMaterialDTO1_1.itemName = "测试物料"
    afterSaleItemMaterialDTO1_1.itemCode = "XPPP232132"
    afterSaleItemMaterialDTO1_1.batch = "123"
    afterSaleItemMaterialDTO1_1.itemProperty = "123"
    
    
    let afterSaleItemMaterialDTO2 = new AfterSaleItemMaterialDTO();
    afterSaleItemMaterialDTO2.itemName = "测试物料"
    afterSaleItemMaterialDTO2.itemCode = "XPPP232132"
    afterSaleItemMaterialDTO2.batch = "4234234"
    await createSerialNumberString().then(resp => {
        afterSaleItemMaterialDTO2.sn = resp
    })
    
    
    let afterSaleItemMaterialDTO2_2 = new AfterSaleItemMaterialDTO();
    
    afterSaleItemMaterialDTO2_2.itemName = "测试物料"
    afterSaleItemMaterialDTO2_2.itemCode = "XPPP232132"
    afterSaleItemMaterialDTO2_2.batch = "123"
    afterSaleItemMaterialDTO2_2.itemProperty = "123"
    
    
    afterSaleItemLineInfoDTO1.inMaterIalInfo = afterSaleItemMaterialDTO
    afterSaleItemLineInfoDTO1.outMaterialInfo = afterSaleItemMaterialDTO1_1
    
    afterSaleItemLineInfoDTO2.inMaterIalInfo = afterSaleItemMaterialDTO2
    afterSaleItemLineInfoDTO2.outMaterialInfo = afterSaleItemMaterialDTO2_2
    formData.itemInfo[0] = afterSaleItemLineInfoDTO1
    formData.itemInfo.push(afterSaleItemLineInfoDTO2)
    
    await createSerialNumber()
}


/**
 * 监听渠道 监听店铺
 *
 * @author 徐寿春
 * 2023/4/12 08:35
 */
const shopSelect = computed(() => {
    return formatterManager.computeFormatterShop(formData.channel)
})

const channelSelect = computed(() => {
    return formatterManager.computeFormatterChannel(formData.shopName)
})


/**
 * 如果是编辑反查询
 */
let formData: UnwrapNestedRefs<AfterSaleOrderInfoOrLineDTO> = reactive(afterSaleOrderInfoOrLineAddOne());


let route = useRoute();
let watchEffect1 = watchEffect(() => {
    formData.afterSaleOrderId = route.query?.id as any
});
watchEffect1()


</script>





